﻿using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.Model;
using Microsoft.ApplicationBlocks.Data;

namespace AnyOffice.general.attend.manage
{
    public partial class search : ModulePage
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            int num6;
            DateTime time4;
            DateTime time5;
            DateTime time6;
            string str3;
            DataTable table4;
            DataTable table5;
            DataTable table6;
            string userId = (Request.QueryString["user_id"] != null) ? Request.QueryString["user_id"].ToString() : base.User.Identity.Name;
            this.ViewState["UserId"] = userId;
            DateTime time = (Request.QueryString["date1"] != null) ? Convert.ToDateTime(Request.QueryString["date1"].ToString()) : DateTime.Now;
            this.ViewState["Date1"] = time.ToString("yyyy-MM-dd");
            DateTime time2 = (Request.QueryString["date2"] != null) ? Convert.ToDateTime(Request.QueryString["date2"].ToString()) : DateTime.Now;
            this.ViewState["Date2"] = time2.ToString("yyyy-MM-dd");
            TimeSpan span = new TimeSpan(time.Ticks);
            TimeSpan ts = new TimeSpan(time2.Ticks);
            int num = span.Subtract(ts).Duration().Days + 1;
            this.litNavTitle.Text = base.ShowMessage("查询 " + new UserCache(userId).UserName + " 从 " + time.ToString("D") + " 至 " + time2.ToString("D") + " 共" + num.ToString() + "天的考勤记录").Replace("180", "330");
            int num2 = 30;
            int num3 = 10;
            int num4 = 10;
            int num5 = 60;
            AttendParaDao dao = new AttendParaDao();
            dao.GetPara();
            num2 = dao.DutyIntervalBefore1;
            num3 = dao.DutyIntervalAfter1;
            num4 = dao.DutyIntervalBefore2;
            num5 = dao.DutyIntervalAfter2;
            DataTable userConfig = new AttendConfigDao().GetUserConfig(base.Conn, userId);
            string str2 = (userConfig.Rows[0]["General"] != DBNull.Value) ? userConfig.Rows[0]["General"].ToString() : "";
            if (str2 != string.Empty)
            {
                str2 = "," + str2 + ",";
            }
            DataTable table2 = new DataTable();
            table2.Columns.Add("DutyType", Type.GetType("System.Int32"));
            table2.Columns.Add("DutyTime", Type.GetType("System.DateTime"));
            table2.Columns.Add("BeginTime", Type.GetType("System.DateTime"));
            table2.Columns.Add("EndTime", Type.GetType("System.DateTime"));
            if (userConfig.Rows[0]["DutyTime1"] != DBNull.Value)
            {
                num6 = Convert.ToInt32(userConfig.Rows[0]["DutyType1"].ToString());
                time4 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + userConfig.Rows[0]["DutyTime1"].ToString());
                if (num6 == 1)
                {
                    time5 = time4.AddMinutes((double)-num2);
                    time6 = time4.AddMinutes((double)num3);
                }
                else
                {
                    time5 = time4.AddMinutes((double)-num4);
                    time6 = time4.AddMinutes((double)num5);
                }
                table2.Rows.Add(new object[] { num6, time4, time5, time6 });
            }
            if (userConfig.Rows[0]["DutyTime2"] != DBNull.Value)
            {
                num6 = Convert.ToInt32(userConfig.Rows[0]["DutyType2"].ToString());
                time4 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + userConfig.Rows[0]["DutyTime2"].ToString());
                if (num6 == 1)
                {
                    time5 = time4.AddMinutes((double)-num2);
                    time6 = time4.AddMinutes((double)num3);
                }
                else
                {
                    time5 = time4.AddMinutes((double)-num4);
                    time6 = time4.AddMinutes((double)num5);
                }
                table2.Rows.Add(new object[] { num6, time4, time5, time6 });
            }
            if (userConfig.Rows[0]["DutyTime3"] != DBNull.Value)
            {
                num6 = Convert.ToInt32(userConfig.Rows[0]["DutyType3"].ToString());
                time4 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + userConfig.Rows[0]["DutyTime3"].ToString());
                if (num6 == 1)
                {
                    time5 = time4.AddMinutes((double)-num2);
                    time6 = time4.AddMinutes((double)num3);
                }
                else
                {
                    time5 = time4.AddMinutes((double)-num4);
                    time6 = time4.AddMinutes((double)num5);
                }
                table2.Rows.Add(new object[] { num6, time4, time5, time6 });
            }
            if (userConfig.Rows[0]["DutyTime4"] != DBNull.Value)
            {
                num6 = Convert.ToInt32(userConfig.Rows[0]["DutyType4"].ToString());
                time4 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + userConfig.Rows[0]["DutyTime4"].ToString());
                if (num6 == 1)
                {
                    time5 = time4.AddMinutes((double)-num2);
                    time6 = time4.AddMinutes((double)num3);
                }
                else
                {
                    time5 = time4.AddMinutes((double)-num4);
                    time6 = time4.AddMinutes((double)num5);
                }
                table2.Rows.Add(new object[] { num6, time4, time5, time6 });
            }
            if (userConfig.Rows[0]["DutyTime5"] != DBNull.Value)
            {
                num6 = Convert.ToInt32(userConfig.Rows[0]["DutyType5"].ToString());
                time4 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + userConfig.Rows[0]["DutyTime5"].ToString());
                if (num6 == 1)
                {
                    time5 = time4.AddMinutes((double)-num2);
                    time6 = time4.AddMinutes((double)num3);
                }
                else
                {
                    time5 = time4.AddMinutes((double)-num4);
                    time6 = time4.AddMinutes((double)num5);
                }
                table2.Rows.Add(new object[] { num6, time4, time5, time6 });
            }
            if (userConfig.Rows[0]["DutyTime6"] != DBNull.Value)
            {
                num6 = Convert.ToInt32(userConfig.Rows[0]["DutyType6"].ToString());
                time4 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + userConfig.Rows[0]["DutyTime6"].ToString());
                if (num6 == 1)
                {
                    time5 = time4.AddMinutes((double)-num2);
                    time6 = time4.AddMinutes((double)num3);
                }
                else
                {
                    time5 = time4.AddMinutes((double)-num4);
                    time6 = time4.AddMinutes((double)num5);
                }
                table2.Rows.Add(new object[] { num6, time4, time5, time6 });
            }
            int num7 = 0;
            int num8 = 0;
            int num9 = 0;
            int num10 = 0;
            int num11 = 0;
            for (int i = 0; i < num; i++)
            {
                bool flag;
                bool flag2;
                int dayOfWeek = (int)time.AddDays((double)i).DayOfWeek;
                if (str2.IndexOf("," + dayOfWeek.ToString() + ",") < 0)
                {
                    flag = true;
                }
                else
                {
                    flag = false;
                }
                str3 = "SELECT [HolidayId] FROM [dbo].[Attend_Holiday] WHERE [BeginDate] <='" + time.AddDays((double)i).ToString("yyyy-MM-dd") + "' AND '" + time.AddDays((double)i).ToString("yyyy-MM-dd") + "' <= [EndDate]";
                if (SqlHelper.ExecuteScalar(base.Conn, CommandType.Text, str3) != null)
                {
                    flag2 = true;
                    flag = false;
                }
                else
                {
                    flag2 = false;
                }
                str3 = "SELECT DutyId,RegisterType,RegisterTime,RegisterFlag,Remark FROM [dbo].[Attend_Duty] WHERE [UserId] = '" + userId + "' AND [RegisterTime] >='" + time.AddDays((double)i).ToString("yyyy-MM-dd") + " 00:00:00' AND [RegisterTime] <= '" + time.AddDays((double)i).ToString("yyyy-MM-dd") + " 23:59:59'";
                DataTable table3 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, str3).Tables[0];
                for (int j = 0; j < table2.Rows.Count; j++)
                {
                    DataRow[] rowArray = table3.Select("RegisterType = " + table2.Rows[j]["DutyType"].ToString() + " AND RegisterTime >='" + time.AddDays((double)i).ToString("yyyy-MM-dd") + " " + Convert.ToDateTime(table2.Rows[j]["BeginTime"].ToString()).ToString("HH:mm:ss") + "' AND RegisterTime <='" + time.AddDays((double)i).ToString("yyyy-MM-dd") + " " + Convert.ToDateTime(table2.Rows[j]["EndTime"].ToString()).ToString("HH:mm:ss") + "'");
                    if (rowArray.Length > 0)
                    {
                        if (Convert.ToInt32(rowArray[0]["RegisterFlag"].ToString()) == 1)
                        {
                            flag = false;
                            num8++;
                        }
                        else if (Convert.ToInt32(rowArray[0]["RegisterFlag"].ToString()) == 2)
                        {
                            flag = false;
                            num10++;
                        }
                    }
                    else if ((str2.IndexOf("," + dayOfWeek.ToString() + ",") < 0) && !flag2)
                    {
                        str3 = "SELECT [OutId] FROM [dbo].[Attend_Out] WHERE [Allow] = 1 AND [UserId] ='" + userId + "' AND [OutTime1] <='" + time.AddDays((double)i).ToString("yyyy-MM-dd") + " " + Convert.ToDateTime(table2.Rows[j]["DutyTime"].ToString()).ToString("HH:mm:ss") + "' AND [OutTime2]>='" + time.AddDays((double)i).ToString("yyyy-MM-dd") + " " + Convert.ToDateTime(table2.Rows[j]["DutyTime"].ToString()).ToString("HH:mm:ss") + "'";
                        table4 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, str3).Tables[0];
                        if (table4.Rows.Count == 0)
                        {
                            str3 = "SELECT [LeaveId] FROM [dbo].[Attend_Leave] WHERE [Allow] = 1 AND [UserId] ='" + userId + "' AND [LeaveDate1] <='" + time.AddDays((double)i).ToString("yyyy-MM-dd") + " " + Convert.ToDateTime(table2.Rows[j]["DutyTime"].ToString()).ToString("HH:mm:ss") + "' AND [LeaveDate2]>='" + time.AddDays((double)i).ToString("yyyy-MM-dd") + " " + Convert.ToDateTime(table2.Rows[j]["DutyTime"].ToString()).ToString("HH:mm:ss") + "'";
                            table5 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, str3).Tables[0];
                            if (table5.Rows.Count == 0)
                            {
                                str3 = "SELECT [EvectionId] FROM [dbo].[Attend_Evection] WHERE [Allow] = 1 AND [UserId] ='" + userId + "' AND [EvectionDate1] <='" + time.AddDays((double)i).ToString("yyyy-MM-dd") + " " + Convert.ToDateTime(table2.Rows[j]["DutyTime"].ToString()).ToString("HH:mm:ss") + "' AND [EvectionDate1]>='" + time.AddDays((double)i).ToString("yyyy-MM-dd") + " " + Convert.ToDateTime(table2.Rows[j]["DutyTime"].ToString()).ToString("HH:mm:ss") + "'";
                                table6 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, str3).Tables[0];
                                if (table6.Rows.Count == 0)
                                {
                                    if (Convert.ToInt32(table2.Rows[j]["DutyType"].ToString()) == 1)
                                    {
                                        flag = false;
                                        num9++;
                                    }
                                    else
                                    {
                                        flag = false;
                                        num11++;
                                    }
                                }
                            }
                        }
                    }
                }
                if (flag)
                {
                    num7++;
                }
            }
            if (num7 != 0)
            {
                this.litAllNum.Text = num7.ToString();
            }
            if (num8 != 0)
            {
                this.litLaterNum.Text = num8.ToString();
            }
            if (num9 != 0)
            {
                this.litUpNum.Text = num9.ToString();
            }
            if (num10 != 0)
            {
                this.litBeforeNum.Text = num10.ToString();
            }
            if (num11 != 0)
            {
                this.litDownNum.Text = num11.ToString();
            }
            time5 = Convert.ToDateTime(time.ToString("yyyy-MM-dd") + " 00:00:00");
            time6 = Convert.ToDateTime(time2.ToString("yyyy-MM-dd") + " 23:59:59");
            str3 = "SELECT [Attend_Out].[OutType],[Attend_Out].[OutTime1],[Attend_Out].[OutTime2],[Attend_Out].[Status],[Users].[UserName] FROM [dbo].[Attend_Out],[dbo].[Users] WHERE [Attend_Out].[Allow] = 1 AND [Attend_Out].[LeaderId] = [Users].[UserId] AND [Attend_Out].[UserId] = '" + userId + "' AND (([Attend_Out].[OutTime1] >= '" + time5.ToString() + "' AND [Attend_Out].[OutTime1] <= '" + time6.ToString() + "') OR ([Attend_Out].[OutTime2] >= '" + time5.ToString() + "' AND [Attend_Out].[OutTime2] <= '" + time6.ToString() + "')) ORDER BY [Attend_Out].[OutId] ASC";
            table4 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, str3).Tables[0];
            if (table4.Rows.Count > 0)
            {
                this.rptOut.DataSource = table4;
                this.rptOut.DataBind();
            }
            else
            {
                this.litOut.Text = base.ShowMessage("无外出记录");
            }
            str3 = "SELECT [Attend_Leave].[LeaveType],[Attend_Leave].[LeaveDate1],[Attend_Leave].[LeaveDate2],[Attend_Leave].[Status],[Users].[UserName] FROM [dbo].[Attend_Leave],[dbo].[Users] WHERE [Attend_Leave].[Allow] = 1 AND [Attend_Leave].[LeaderId] = [Users].[UserId] AND [Attend_Leave].[UserId] = '" + userId + "' AND (([Attend_Leave].[LeaveDate1] >= '" + time5.ToString() + "' AND [Attend_Leave].[LeaveDate1] <= '" + time6.ToString() + "') OR ([Attend_Leave].[LeaveDate2] >= '" + time5.ToString() + "' AND [Attend_Leave].[LeaveDate2] <= '" + time6.ToString() + "')) ORDER BY [Attend_Leave].[LeaveId] ASC";
            table5 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, str3).Tables[0];
            if (table5.Rows.Count > 0)
            {
                this.rptLeave.DataSource = table5;
                this.rptLeave.DataBind();
            }
            else
            {
                this.litLeave.Text = base.ShowMessage("无请假记录");
            }
            str3 = "SELECT [Attend_Evection].[EvectionDest],[Attend_Evection].[EvectionDate1],[Attend_Evection].[EvectionDate2],[Attend_Evection].[Status],[Users].[UserName] FROM [dbo].[Attend_Evection],[dbo].[Users] WHERE [Attend_Evection].[Allow] = 1 AND [Attend_Evection].[LeaderId] = [Users].[UserId] AND [Attend_Evection].[UserId] = '" + userId + "' AND (([Attend_Evection].[EvectionDate1] >= '" + time5.ToString() + "' AND [Attend_Evection].[EvectionDate1] <= '" + time6.ToString() + "') OR ([Attend_Evection].[EvectionDate2] >= '" + time5.ToString() + "' AND [Attend_Evection].[EvectionDate2] <= '" + time6.ToString() + "')) ORDER BY [Attend_Evection].[EvectionId] ASC";
            table6 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, str3).Tables[0];
            if (table6.Rows.Count > 0)
            {
                this.rptEvection.DataSource = table6;
                this.rptEvection.DataBind();
            }
            else
            {
                this.litEvection.Text = base.ShowMessage("无出差记录");
            }
        }

    }
}
